﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using TYAJZWeb.Utils;
using TYAJZWeb.Data;
using TYAJZWeb.Models;

namespace TYAJZWeb.Controllers
{
    public class EquipmentModelController : BaseController
    {
        [CookiesAuthorize]
        public ActionResult List(int page)
        {
            var r = Context.EquipmentModels.Where(t => t.IsDeleted == false)
                .OrderByDescending(t => t.Id)
                .Select(t => (EquipmentModelListModel)t);
            int totalCount = r.Count();
            r = r.Skip((page - 1) * PAGESIZE).Take(PAGESIZE);
            return View(new PageList<EquipmentModelListModel>(r, page, PAGESIZE, totalCount));
        }

        [CookiesAuthorize]
        [HttpGet]
        public ActionResult Add()
        {
            return View("Edit");
        }

        [CookiesAuthorize]
        [HttpPost]
        public ActionResult Add(EquipmentModelEditModel model)
        {
            if (ModelState.IsValid)
            {
                EquipmentModel em = (EquipmentModel)model;
                em.IsDeleted = false;
                em.CreateDT = DateTime.Now;
                em.LastModifiedDT = DateTime.Now;
                em.LastModifiedUser = CurrentUserId;
                Context.EquipmentModels.InsertOnSubmit(em);
                Context.SubmitChanges();

                TempData.Add("Success", "添加新设备年限成功");
                return RedirectToAction("List");
            }
            return View("Edit", model);
        }

        [CookiesAuthorize]
        [HttpGet]
        public ActionResult Edit(int id)
        {
            var em = Context.EquipmentModels.SingleOrDefault(t => t.Id == id);
            if (em != null)
            {
                return View("Edit", (EquipmentModelEditModel)em);
            }
            return View("Error");
        }

        [CookiesAuthorize]
        [HttpPost]
        public ActionResult Edit(EquipmentModelEditModel model)
        {
            if (ModelState.IsValid)
            {
                EquipmentModel em = Context.EquipmentModels.SingleOrDefault(t => t.Id == model.Id);
                em.LimitedMonth = model.LimitedYear * 12;
                em.LastModifiedDT = DateTime.Now;
                em.LastModifiedUser = CurrentUserId;
                Context.SubmitChanges();

                TempData.Add("Success", "修改新设备年限成功");
                return RedirectToAction("List");
            }
            return View("Edit", model);
        }

        [CookiesAuthorize]
        [HttpGet]
        public ActionResult Delete(int id)
        {
            EquipmentModel em = Context.EquipmentModels.SingleOrDefault(t => t.Id == id);
            em.IsDeleted = true;
            Context.SubmitChanges();
            TempData.Add("Success", "删除设备年限成功");
            return RedirectToAction("List");
        }

        [CookiesAuthorize]
        [HttpGet]
        public JsonResult Suggestion(String term)
        {
            var list = Context.EquipmentModels.Where(t => t.Name.Contains(term))
                .Select(t => new SuggestionModel<String>()
                {
                    label = t.Name,
                    Value = t.Name,
                    Data = (t.LimitedMonth / 12).ToString()
                }).ToList();

            return new JsonResult() { Data = list, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }
    }
}
